package com.azxc.rapid.plus.user.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.azxc.rapid.core.log.exception.ServiceException;
import com.azxc.rapid.plus.user.entity.WestoneSectors;
import com.azxc.rapid.plus.user.entity.WestoneStaff;
import com.azxc.rapid.plus.user.entity.WestoneVersion;
import com.azxc.rapid.plus.user.mapper.WestoneSectorsMapper;
import com.azxc.rapid.plus.user.mapper.WestoneVersionMapper;
import com.azxc.rapid.plus.user.service.IWestoneSectorsService;
import com.azxc.rapid.plus.user.service.IWestoneStaffService;
import com.azxc.rapid.plus.user.service.IWestoneVersionService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linym
 * @className: WestoneSectorsServiceImpl
 * @description:
 * @date 2023/1/30 12:39
 * @version：1.0
 */
@Service
public class WestoneVersionServiceImpl extends ServiceImpl<WestoneVersionMapper, WestoneVersion> implements IWestoneVersionService {


	@Autowired
	private IWestoneSectorsService westoneSectorsService;

	@Autowired
	private IWestoneStaffService westoneStaffService;

	@Value("${westone.appKey:APPAD3BADB33B0622F1BE4C0E5A}")
	private  String appKey;
	@Value("${westone.corpId:787987360344064}")
	private  String	corpId;

	@Value("${westone.corpSecret:a1024mo37JlrK3u2_MR7yNv8rKXsGD819_sNsrJZ4Ubg0zSi5y_0k568xPRQ7TIe951}")
	private  String  corpSecret;

	@Override
	@Transactional
	public boolean synchronizationAll() {
		String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		String r = HttpUtil.get(url);
		JSONObject jobject = JSONObject.parseObject(r);

		String url2 = "https://cx-service.westonecloud.com/api/apps/v1/address-book?access_token="+jobject.getString("access_token");
		r = HttpUtil.get(url2);
		jobject = JSONObject.parseObject(r);
		System.out.println(jobject);
		//全量更新，清空原有数据

		Wrapper<WestoneVersion> deteWrapper =new LambdaQueryWrapper<>();
		this.remove(deteWrapper);
		Wrapper<WestoneSectors> deleteQuery = new LambdaQueryWrapper<>();
		westoneSectorsService.remove(deleteQuery);
		Wrapper<WestoneStaff> deleteQuery2 = new LambdaQueryWrapper<>();
		westoneStaffService.remove(deleteQuery2);

		WestoneVersion westoneVersion =	JSONObject.toJavaObject(jobject,WestoneVersion.class);
		List<WestoneSectors> sectorsList=new ArrayList<>();
		JSONArray array= jobject.getJSONArray("sectors");
		for(int i=0;i<array.size();i++){
			WestoneSectors westoneSectors =	JSONObject.toJavaObject(array.getJSONObject(i),WestoneSectors.class);
			sectorsList.add(westoneSectors);
		}
		List<WestoneStaff> staffList=new ArrayList<>();
		JSONArray array2= jobject.getJSONArray("staffs");
		for(int i=0;i<array2.size();i++){
			WestoneStaff westoneStaff =	JSONObject.toJavaObject(array2.getJSONObject(i),WestoneStaff.class);
			westoneStaff.setSectorId(array2.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("sector_id"));
			westoneStaff.setShowOrder(array2.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("show_order"));
			staffList.add(westoneStaff);
		}
		this.save(westoneVersion);
		westoneSectorsService.saveBatch(sectorsList);
		westoneStaffService.saveBatch(staffList);


		return true;
	}

	@Override
	@Transactional
	public boolean addChange() {
		//查询当前版本号后的数据
		List<WestoneVersion> list=this.list();
		if(CollectionUtils.isEmpty(list)){
			throw new ServiceException("没有版本号，请先进行全量同步");
		}
		WestoneVersion westoneVersion=	list.get(0);
		westoneVersion.getVersion();

		String url = "https://cx-service.westonecloud.com/api/apps/v1/access-tokens?app_key=" + appKey + "&corp_id=" + corpId + "&corp_secret=" + corpSecret;
		String r = HttpUtil.get(url);
		JSONObject jobject = JSONObject.parseObject(r);
		String url2 ="https://cx-service.westonecloud.com/api/apps/v1/address-book/changes/"+westoneVersion.getVersion()+"?access_token="+jobject.getString("access_token");
		String r2 = HttpUtil.get(url2);
		jobject = JSONObject.parseObject(r2);
		WestoneVersion newWestoneVersion =	JSONObject.toJavaObject(jobject,WestoneVersion.class);

		List<WestoneSectors> addSectorsList=new ArrayList<>();
		JSONArray array= jobject.getJSONArray("added_sectors");
		for(int i=0;i<array.size();i++){
			WestoneSectors westoneSectors =	JSONObject.toJavaObject(array.getJSONObject(i),WestoneSectors.class);
			addSectorsList.add(westoneSectors);
		}

		List<WestoneSectors> updateSectorsList=new ArrayList<>();
		JSONArray array2= jobject.getJSONArray("updated_sectors");
		for(int i=0;i<array2.size();i++){
			WestoneSectors westoneSectors =	JSONObject.toJavaObject(array.getJSONObject(i),WestoneSectors.class);
			updateSectorsList.add(westoneSectors);
		}
		JSONArray array3= jobject.getJSONArray("deleted_sectors");
		List<String> deleSectorsList=new ArrayList<>();
		for(int i=0;i<array3.size();i++){
			deleSectorsList.add(array3.getString(i));
		}

		List<WestoneStaff> addStaffList=new ArrayList<>();
		JSONArray array4= jobject.getJSONArray("added_staffs");
		for(int i=0;i<array4.size();i++){
			WestoneStaff westoneStaff =	JSONObject.toJavaObject(array4.getJSONObject(i),WestoneStaff.class);
			westoneStaff.setSectorId(array4.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("sector_id"));
			westoneStaff.setShowOrder(array4.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("show_order"));
			addStaffList.add(westoneStaff);
		}

		List<WestoneStaff> updateStaffList=new ArrayList<>();
		JSONArray array5= jobject.getJSONArray("updated_staffs");
		for(int i=0;i<array5.size();i++){
			WestoneStaff westoneStaff =	JSONObject.toJavaObject(array5.getJSONObject(i),WestoneStaff.class);
			westoneStaff.setSectorId(array5.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("sector_id"));
			westoneStaff.setShowOrder(array5.getJSONObject(i).getJSONArray("sector_staffs").getJSONObject(0).getString("show_order"));
			updateStaffList.add(westoneStaff);
		}

		JSONArray array6= jobject.getJSONArray("deleted_staffs");
		List<String> deleStaffList=new ArrayList<>();
		for(int i=0;i<array6.size();i++){
			deleStaffList.add(array6.getString(i));
		}

		this.updateById(newWestoneVersion);
		westoneSectorsService.saveBatch(addSectorsList);
		westoneSectorsService.updateBatchById(updateSectorsList);
		if(!CollectionUtils.isEmpty(deleSectorsList)){
			LambdaQueryWrapper<WestoneSectors> deleteWrapper = new LambdaQueryWrapper<>();
			deleteWrapper.in(WestoneSectors::getId,deleSectorsList);
			westoneSectorsService.remove(deleteWrapper);
		}

		westoneStaffService.saveBatch(addStaffList);
		westoneStaffService.updateBatchById(updateStaffList);
		if(!CollectionUtils.isEmpty(deleStaffList)){
			LambdaQueryWrapper<WestoneStaff> deleteWrapper2 = new LambdaQueryWrapper<>();
			deleteWrapper2.in(WestoneStaff::getId,deleStaffList);
			westoneStaffService.remove(deleteWrapper2);
		}
		return true;
	}
}
